package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.weixin.client.WxMemoryCacheClient;
import ysq.xyg.model.WxAccount;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.model.info.WxAccountInfo;
import ysq.xyg.service.BaseMenuService;
import ysq.xyg.service.WxAccountService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:15
 **/
@Controller
@RequestMapping("/wxAccount")
public class WxAccountController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(WxAccountController.class);
    @Resource
    private WxAccountService wxAccountService;
    @Resource
    private BaseMenuService baseMenuService;
    
    private WxAccount wxAccount;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response,String menuid){
        return new ModelAndView("/wxAccountList");
    }

    @RequestMapping(value = "/JsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject JsonList(WxAccount wxAccount, Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<WxAccount> pageInfo=wxAccountService.pageInfo(wxAccount,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        if(pageInfo !=null && pageInfo.getList() !=null && pageInfo.getList().size() > 0){
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for(WxAccount item:pageInfo.getList()){
                row=new JSONObject();
                row.put("id",item.getId());
                row.put("appid",item.getAppid());
                row.put("agentid",item.getAgentid());
                row.put("appsecret",item.getAppsecret());
                row.put("token",item.getToken());
                row.put("payKey",item.getPayKey());
                row.put("payMchId",item.getPayMchId());
                row.put("url",item.getUrl());
                row.put("baseURL",item.getBaseURL());
                row.put("createTime", DateUtil.DateToStr(item.getCreateTime(), DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/SelectEntity",method = RequestMethod.GET)
    public ModelAndView SelectEntity(String id){
        ModelAndView view=new ModelAndView("/wxAccountUpdateOrSave");
        WxAccount wxAccount=wxAccountService.selectByPrimaryKey(id);
        view.addObject("wxAccount",wxAccount);
        return view;
    }

    @RequestMapping(value = "/Show",method = RequestMethod.GET)
    public ModelAndView Show(String id){
        ModelAndView view=new ModelAndView("/wxAccountShow");
        WxAccount wxAccount=wxAccountService.selectByPrimaryKey(id);
        view.addObject("wxAccount",wxAccount);
        return view;
    }

    @RequestMapping(value = "/UpdateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject UpdateOrAddEntity(WxAccountInfo wxAccountInfo){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(wxAccountInfo.getId()==null || "".equals(wxAccountInfo.getId())){
            wxAccount=new WxAccount();
            wxAccount.setId(BaseUtil.uId());
            int le=wxAccount.getId().length();
            wxAccount.setCreateTime(new Date());
            wxAccount.setAgentid(wxAccountInfo.getAgentid());
            wxAccount.setAppid(wxAccountInfo.getAppid());
            wxAccount.setAppsecret(wxAccountInfo.getAppsecret());
            wxAccount.setToken(wxAccountInfo.getToken());
            wxAccount.setBaseURL(wxAccountInfo.getBaseURL());
            wxAccount.setPayKey(wxAccountInfo.getPayKey());
            wxAccount.setPayMchId(wxAccountInfo.getPayMchId());
            wxAccount.setXcxAppSecret(wxAccountInfo.getXcxAppSecret());
            wxAccount.setXcxAppId(wxAccountInfo.getXcxAppId());
            wxAccount.setXcxAccessTokenEndTime(wxAccountInfo.getXcxAccessTokenEndTime());
            int id=wxAccountService.insert(wxAccount);
            if(id <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);

            }else{
                WxMemoryCacheClient.deleteSingleWxAccountInfo();
            }
        }else{
            wxAccount=wxAccountService.selectByPrimaryKey(wxAccountInfo.getId());
            wxAccount.setCreateTime(new Date());
            wxAccount.setAppid(wxAccountInfo.getAppid());
            wxAccount.setAppsecret(wxAccountInfo.getAppsecret());
            wxAccount.setToken(wxAccountInfo.getToken());
            wxAccount.setBaseURL(wxAccountInfo.getBaseURL());
            wxAccount.setPayKey(wxAccountInfo.getPayKey());
            wxAccount.setAgentid(wxAccountInfo.getAgentid());
            wxAccount.setPayMchId(wxAccountInfo.getPayMchId());
            wxAccount.setXcxAppSecret(wxAccountInfo.getXcxAppSecret());
            wxAccount.setXcxAppId(wxAccountInfo.getXcxAppId());
            wxAccount.setXcxAccessTokenEndTime(null);
            wxAccount.setAccessTokenEndTime(null);
            wxAccount.setJsTicketEndTime(null);
            wxAccount.setAccessToken("");
            wxAccount.setXcxAccessToken("");
            wxAccount.setJsTicket("");
            int id=wxAccountService.updateByPrimaryKey(wxAccount);
            if(id <= 0){
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }else{
                WxMemoryCacheClient.deleteSingleWxAccountInfo();
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }


}
